<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>

      <div v-if="isOpen">
        <el-form-item label="成本价" prop="costPrice">
          <el-input
            v-model="queryParams.costPrice"
            placeholder="请输入成本价"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="成本价差异" prop="costPriceDifference">
          <el-input
            v-model="queryParams.costPriceDifference"
            placeholder="请输入成本价差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="期末金额" prop="endAmount">
          <el-input
            v-model="queryParams.endAmount"
            placeholder="请输入期末金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="期末金额差异" prop="endAmountDifference">
          <el-input
            v-model="queryParams.endAmountDifference"
            placeholder="请输入期末金额差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="期初+采购" prop="initialPurchase">
          <el-input
            v-model="queryParams.initialPurchase"
            placeholder="请输入期初+采购"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="期初+采购差异" prop="initialPurchaseDifference">
          <el-input
            v-model="queryParams.initialPurchaseDifference"
            placeholder="请输入期初+采购差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="盘点" prop="inventoryCount">
          <el-input
            v-model="queryParams.inventoryCount"
            placeholder="请输入盘点"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="盘点差异" prop="inventoryCountDiffernece">
          <el-input
            v-model="queryParams.inventoryCountDiffernece"
            placeholder="请输入盘点差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS成本价" prop="misCostPrice">
          <el-input
            v-model="queryParams.misCostPrice"
            placeholder="请输入MIS成本价"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS期末金额" prop="misEndAmount">
          <el-input
            v-model="queryParams.misEndAmount"
            placeholder="请输入MIS期末金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS期初+采购" prop="misInitialPurchase">
          <el-input
            v-model="queryParams.misInitialPurchase"
            placeholder="请输入MIS期初+采购"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS盘点" prop="misInventoryCount">
          <el-input
            v-model="queryParams.misInventoryCount"
            placeholder="请输入MIS盘点"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS其他出入库" prop="misOtherInventory">
          <el-input
            v-model="queryParams.misOtherInventory"
            placeholder="请输入MIS其他出入库"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS销售出库" prop="misSalesOutstock">
          <el-input
            v-model="queryParams.misSalesOutstock"
            placeholder="请输入MIS销售出库"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MIS销售退货" prop="misSalesReturn">
          <el-input
            v-model="queryParams.misSalesReturn"
            placeholder="请输入MIS销售退货"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-input
            v-model="queryParams.month"
            placeholder="请输入月份"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="其他出入库" prop="otherInventory">
          <el-input
            v-model="queryParams.otherInventory"
            placeholder="请输入其他出入库"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="其他出入库差异" prop="otherInventoryDifference">
          <el-input
            v-model="queryParams.otherInventoryDifference"
            placeholder="请输入其他出入库差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="销售出库" prop="salesOutstock">
          <el-input
            v-model="queryParams.salesOutstock"
            placeholder="请输入销售出库"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售出库差异" prop="salesOutstockDifference">
          <el-input
            v-model="queryParams.salesOutstockDifference"
            placeholder="请输入销售出库差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售退货" prop="salesReturn">
          <el-input
            v-model="queryParams.salesReturn"
            placeholder="请输入销售退货"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售退货差异" prop="salesReturnDifference">
          <el-input
            v-model="queryParams.salesReturnDifference"
            placeholder="请输入销售退货差异"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input
            v-model="queryParams.year"
            placeholder="请输入年份"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:financialCheck:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:financialCheck:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:financialCheck:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:financialCheck:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="financialCheckList"
      @selection-change="handleSelectionChange"
      :height="650"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="productCode"
        label="商品编码"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="costPrice"
        label="成本价"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misCostPrice"
        label="MIS成本价"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="costPriceDifference"
        label="成本价差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="initialPurchase"
        label="期初+采购"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misInitialPurchase"
        label="MIS期初+采购"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="initialPurchaseDifference"
        label="期初+采购差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="salesOutstock"
        label="销售出库"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misSalesOutstock"
        label="MIS销售出库"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="salesOutstockDifference"
        label="销售出库差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="salesReturn"
        label="销售退货"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="salesReturnDifference"
        label="销售退货差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misSalesReturn"
        label="MIS销售退货"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="otherInventory"
        label="其他出入库"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misOtherInventory"
        label="MIS其他出入库"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="otherInventoryDifference"
        label="其他出入库差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="inventoryCount"
        label="盘点"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misInventoryCount"
        label="MIS盘点"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="inventoryCountDiffernece"
        label="盘点差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="endAmount"
        label="期末金额"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="misEndAmount"
        label="MIS期末金额"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column
        prop="endAmountDifference"
        label="期末金额差异"
        width="120"
        numeric
      ></el-table-column>
      <el-table-column prop="year" label="年份" width="80"></el-table-column>
      <el-table-column prop="month" label="月份" width="80"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:financialCheck:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['financial:financialCheck:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务进销存数据核对对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="成本价" prop="costPrice">
          <el-input v-model="form.costPrice" placeholder="请输入成本价" />
        </el-form-item>
        <el-form-item label="成本价差异" prop="costPriceDifference">
          <el-input
            v-model="form.costPriceDifference"
            placeholder="请输入成本价差异"
          />
        </el-form-item>
        <el-form-item label="期末金额" prop="endAmount">
          <el-input v-model="form.endAmount" placeholder="请输入期末金额" />
        </el-form-item>
        <el-form-item label="期末金额差异" prop="endAmountDifference">
          <el-input
            v-model="form.endAmountDifference"
            placeholder="请输入期末金额差异"
          />
        </el-form-item>
        <el-form-item label="期初+采购" prop="initialPurchase">
          <el-input
            v-model="form.initialPurchase"
            placeholder="请输入期初+采购"
          />
        </el-form-item>
        <el-form-item label="期初+采购差异" prop="initialPurchaseDifference">
          <el-input
            v-model="form.initialPurchaseDifference"
            placeholder="请输入期初+采购差异"
          />
        </el-form-item>
        <el-form-item label="盘点" prop="inventoryCount">
          <el-input v-model="form.inventoryCount" placeholder="请输入盘点" />
        </el-form-item>
        <el-form-item label="盘点差异" prop="inventoryCountDiffernece">
          <el-input
            v-model="form.inventoryCountDiffernece"
            placeholder="请输入盘点差异"
          />
        </el-form-item>
        <el-form-item label="MIS成本价" prop="misCostPrice">
          <el-input v-model="form.misCostPrice" placeholder="请输入MIS成本价" />
        </el-form-item>
        <el-form-item label="MIS期末金额" prop="misEndAmount">
          <el-input
            v-model="form.misEndAmount"
            placeholder="请输入MIS期末金额"
          />
        </el-form-item>
        <el-form-item label="MIS期初+采购" prop="misInitialPurchase">
          <el-input
            v-model="form.misInitialPurchase"
            placeholder="请输入MIS期初+采购"
          />
        </el-form-item>
        <el-form-item label="MIS盘点" prop="misInventoryCount">
          <el-input
            v-model="form.misInventoryCount"
            placeholder="请输入MIS盘点"
          />
        </el-form-item>
        <el-form-item label="MIS其他出入库" prop="misOtherInventory">
          <el-input
            v-model="form.misOtherInventory"
            placeholder="请输入MIS其他出入库"
          />
        </el-form-item>
        <el-form-item label="MIS销售出库" prop="misSalesOutstock">
          <el-input
            v-model="form.misSalesOutstock"
            placeholder="请输入MIS销售出库"
          />
        </el-form-item>
        <el-form-item label="MIS销售退货" prop="misSalesReturn">
          <el-input
            v-model="form.misSalesReturn"
            placeholder="请输入MIS销售退货"
          />
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-input v-model="form.month" placeholder="请输入月份" />
        </el-form-item>
        <el-form-item label="其他出入库" prop="otherInventory">
          <el-input
            v-model="form.otherInventory"
            placeholder="请输入其他出入库"
          />
        </el-form-item>
        <el-form-item label="其他出入库差异" prop="otherInventoryDifference">
          <el-input
            v-model="form.otherInventoryDifference"
            placeholder="请输入其他出入库差异"
          />
        </el-form-item>
        <el-form-item label="商品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="销售出库" prop="salesOutstock">
          <el-input v-model="form.salesOutstock" placeholder="请输入销售出库" />
        </el-form-item>
        <el-form-item label="销售出库差异" prop="salesOutstockDifference">
          <el-input
            v-model="form.salesOutstockDifference"
            placeholder="请输入销售出库差异"
          />
        </el-form-item>
        <el-form-item label="销售退货" prop="salesReturn">
          <el-input v-model="form.salesReturn" placeholder="请输入销售退货" />
        </el-form-item>
        <el-form-item label="销售退货差异" prop="salesReturnDifference">
          <el-input
            v-model="form.salesReturnDifference"
            placeholder="请输入销售退货差异"
          />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFinancialCheck,
  getFinancialCheck,
  delFinancialCheck,
  addFinancialCheck,
  updateFinancialCheck,
} from "@/api/financial/financialCheck";

export default {
  name: "FinancialCheck",
  data() {
    return {
      isOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务进销存数据核对表格数据
      financialCheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        costPrice: null,
        costPriceDifference: null,
        endAmount: null,
        endAmountDifference: null,
        initialPurchase: null,
        initialPurchaseDifference: null,
        inventoryCount: null,
        inventoryCountDiffernece: null,
        misCostPrice: null,
        misEndAmount: null,
        misInitialPurchase: null,
        misInventoryCount: null,
        misOtherInventory: null,
        misSalesOutstock: null,
        misSalesReturn: null,
        month: null,
        otherInventory: null,
        otherInventoryDifference: null,
        productCode: null,
        salesOutstock: null,
        salesOutstockDifference: null,
        salesReturn: null,
        salesReturnDifference: null,
        year: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  beforeDestroy() {
    this.$emit("deleteAll");
  },
  methods: {
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    /** 查询财务进销存数据核对列表 */
    getList() {
      this.loading = true;
      listFinancialCheck(this.queryParams).then((response) => {
        this.financialCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        costPrice: null,
        costPriceDifference: null,
        endAmount: null,
        endAmountDifference: null,
        id: null,
        initialPurchase: null,
        initialPurchaseDifference: null,
        inventoryCount: null,
        inventoryCountDiffernece: null,
        misCostPrice: null,
        misEndAmount: null,
        misInitialPurchase: null,
        misInventoryCount: null,
        misOtherInventory: null,
        misSalesOutstock: null,
        misSalesReturn: null,
        month: null,
        otherInventory: null,
        otherInventoryDifference: null,
        productCode: null,
        salesOutstock: null,
        salesOutstockDifference: null,
        salesReturn: null,
        salesReturnDifference: null,
        year: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加财务进销存数据核对";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFinancialCheck(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改财务进销存数据核对";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFinancialCheck(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFinancialCheck(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除财务进销存数据核对编号为"' + ids + '"的数据项？')
        .then(function () {
          return delFinancialCheck(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "financial/financialCheck/export",
        {
          ...this.queryParams,
        },
        `financialCheck_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
